<?php
namespace org\net;

class RongCloud {

    private $AccountSid;
    private $AccountToken;
    private $AppId;
    private $ServerIP = 'app.cloopen.com';
    private $ServerPort = '8883';
    private $SoftVersion = '2013-12-26';
    private $Batch;  //时间戳
    private $BodyType = "json";//包体格式，可填值：json 、xml
    private $enabeLog = true; //日志开关。可填值：true

    function __construct($AccountSid, $AccountToken, $AppId) {
        $this->AccountSid = $AccountSid; //主帐号,对应开官网发者主账号下的 ACCOUNT SID
        $this->AccountToken = $AccountToken; //主帐号令牌,对应官网开发者主账号下的 AUTH TOKEN
        $this->AppId = $AppId; //应用Id，在官网应用列表中点击应用，对应应用详情中的APP ID
        $this->Batch = date("YmdHis");
    }
    /**
     * 打印日志
     * @param log 日志内容
     */
    private function showlog($log){
        if($this->enabeLog){
            trace($log,'sms');
        }
    }
    /**
     * 发起HTTPS请求
     */
    private function curl_post($url, $data, $header, $post = 1) {
        //初始化curl
        $ch = curl_init();
        //参数设置
        $res = curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_POST, $post);
        if ($post){
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        $result = curl_exec($ch);
        //连接失败
        if ($result == FALSE) {
            if ($this->BodyType == 'json') {
                $result = "{\"statusCode\":\"172001\",\"statusMsg\":\"网络错误\"}";
            } else {
                $result = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><Response><statusCode>172001</statusCode><statusMsg>网络错误</statusMsg></Response>";
            }
        }
        curl_close($ch);
        return $result;
    }

    /**
     * 发送模板短信
     * @param string $to 短信接收彿手机号码集合,用英文逗号分开
     * @param array $datas 内容数据
     * @param string $tempId 模板Id
     */
    function sendTemplateSMS($to, $datas, $tempId) {
        // 拼接请求包体
        if ($this->BodyType == "json") {
            $data = "";
            for ($i = 0; $i < count($datas); $i++) {
                $data = $data . "'" . $datas[$i] . "',";
            }
            $body = "{'to':'$to','templateId':'$tempId','appId':'$this->AppId','datas':[" . $data . "]}";
        } else {
            $data = "";
            for ($i = 0; $i < count($datas); $i++) {
                $data = $data . "<data>" . $datas[$i] . "</data>";
            }
            $body = "<TemplateSMS>
                    <to>$to</to> 
                    <appId>$this->AppId</appId>
                    <templateId>$tempId</templateId>
                    <datas>" . $data . "</datas>
                  </TemplateSMS>";
        }
        // 大写的sig参数
        $sig = strtoupper(md5($this->AccountSid . $this->AccountToken . $this->Batch));
        // 生成请求URL
        $url = "https://$this->ServerIP:$this->ServerPort/$this->SoftVersion/Accounts/$this->AccountSid/SMS/TemplateSMS?sig=$sig";
        $this->showlog("请求地址:".$url);
        // 生成授权：主帐户Id + 英文冒号 + 时间戳。
        $authen = base64_encode($this->AccountSid . ":" . $this->Batch);
        // 生成包头
        $header = array("Accept:application/$this->BodyType", "Content-Type:application/$this->BodyType;charset=utf-8", "Authorization:$authen");
        // 发送请求
        $this->showlog("发送内容:".$body);
        $result = $this->curl_post($url, $body, $header);
        $this->showlog("返回结果:".$result);
        if ($this->BodyType == "json") {//JSON格式
            $datas = json_decode($result);
        } else { //xml格式
            $datas = simplexml_load_string(trim($result, " \t\n\r"));
        }
        //重新装填数据
        if ($datas->statusCode == 0) {
            if ($this->BodyType == "json") {
                $datas->TemplateSMS = $datas->templateSMS;
                unset($datas->templateSMS);
            }
        }
        return $datas;
    }
}